package com.antis.olsl.library.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.antis.olsl.library.R;
import com.antis.olsl.library.adapter.SingleAdapter;
import com.antis.olsl.library.bean.MenuInfo;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: ATS_OLSL
 * @Package: com.antis.olsl.dialog
 * @ClassName: ChooseFiledTypeDialog
 * @Description: java类作用描述
 * @Author: 羊琪
 * @CreateDate: 2020/3/24 16:25
 * @UpdateDate: 2020/3/24 16:25
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class ChooseTypeDialog {
    private Context context;

    private ChooseTypeListener listener;
    private Dialog dialog;

    private ArrayList<MenuInfo> filedInfos = new ArrayList<>();
    private ArrayList<MenuInfo> allInfos = new ArrayList<>();
    private SingleAdapter mAdapter;
    private String title = "";

    private int selectedIndex = 0;

    public ChooseTypeDialog(Context context, ArrayList<MenuInfo> filedInfos, ChooseTypeListener listener) {
        if (filedInfos == null) {
            return;
        }
        this.context = context;
        this.filedInfos = filedInfos;
        this.allInfos = filedInfos;
        this.listener = listener;
        initView();
    }

    public ChooseTypeDialog(Context context, String title, ArrayList<MenuInfo> filedInfos, ChooseTypeListener listener) {
        if (filedInfos == null) {
            return;
        }
        this.context = context;
        this.title = title;
        this.filedInfos = filedInfos;
        this.allInfos = filedInfos;
        this.listener = listener;
        initView();
    }

    private void initView() {
        dialog = new Dialog(context, R.style.dialog);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_choose_type, null);
        RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
        mAdapter = new SingleAdapter(R.layout.listitem_choose_filed_type, filedInfos);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                MenuInfo menuInfo = filedInfos.get(position);
                String newId = menuInfo.id;
                for (MenuInfo info : allInfos) {
                    String oldId = info.id;
                    info.choose = newId.equals(oldId);
                }
                //刷新数据
                selectedIndex = position;
                notifyData(allInfos);
            }
        });

        dialog.setContentView(view);
        Window win = dialog.getWindow();
        if (null != win) {
            win.getDecorView().setPadding(0, 0, 0, 0);
            WindowManager.LayoutParams lp = win.getAttributes();
            lp.width = WindowManager.LayoutParams.FILL_PARENT;
            // lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            win.setAttributes(lp);
        }

        view.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        view.findViewById(R.id.ll_top).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

        TextView tvSure = view.findViewById(R.id.tv_sure);
        tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onChooseTypeListener(selectedIndex);
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    /**
     * 刷新数据
     *
     * @param list
     */
    public void notifyData(List<MenuInfo> list) {
        mAdapter.setList(list);
    }

    public void dismiss() {
        if (null != dialog) {
            dialog.dismiss();
        }
    }

    public interface ChooseTypeListener {
        void onChooseTypeListener(int selectedIndex);
    }

}
